<template>
  <div class="dashboard-container">
    <el-card class="box-card">
      <h1 class="title">管理员设置</h1>
      <el-form ref="form" :model="form" label-width="140px" :rules="Rules">
        <el-form-item label="sip服务器" prop="registration_server">
          <el-input v-model="form.registration_server"></el-input>
        </el-form-item>
        <el-form-item label="sip服务器端口" prop="registration_server_port">
          <el-input v-model="form.registration_server_port"></el-input>
        </el-form-item>
        <el-form-item label="sip服务器密码更改" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="sipButton">
          <el-button type="primary" @click="onSubmit('form')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { sipAdmin } from "@/api/sip";
import { Message } from "element-ui";
export default {
  name: "Dashboard",
  data() {
    return {
      form: {
        registration_server: "", //sip服务器
        registration_server_port: "", //sip服务器端口
        password: "",
      },
      Rules: {
        registration_server: [
          { required: true, message: "sip服务器地址不能为空", trigger: "blur" },
        ],
        registration_server_port: [
          { required: true, message: "sip服务器端口不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async onSubmit(formName) {
      let resvalidate;
      // 获取表单校验布尔值 校验参数
      this.$refs[formName].validate((valid) => {
        resvalidate = valid;
      });
      // 判断
      if (resvalidate === false) {
        return;
      }
      // 确定提交 和发起请求
      const res = await sipAdmin(this.form);
      // 没有更改数据
      if (res.status === 201) {
        Message.warning(res.data.data);
      }
      // 成功
      if (res.status === 200) {
        // 清空数据
        (this.form = {
          registration_server: "", //sip服务器
          registration_server_port: "", //sip服务器端口
          password: "",
        }),
          Message.success(res.data.meta.msg);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.sipButton {
  display: flex;
  justify-content: center;
}
.title {
  text-align: center;
}
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
